const path = require('path');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const { paths } = require('../../utils');

module.exports = function (options) {
  let { esLint } = options;
  let isProd = process.env.mode === 'production';
  let isServerHot = process.env.mode === 'serverHot';

  function jsLoader() {
    let defaultArr = [
      // {
      //   loader: 'cache-loader',
      //   options: {
      //     cacheDirectory: path.resolve(paths.roots, 'node_modules/.cache-loader')
      //   }
      // },
      {
        loader: 'babel-loader?cacheDirectory',
      }
    ];
    if (isServerHot) {
      defaultArr.push('react-hot-loader/webpack')
    }
    return defaultArr;
  }

  let group = [
    {
      test: /config\.font\.js$/,
      use: [
        MiniCssExtractPlugin.loader,
        'css-loader',
        {
          loader: 'webfonts-loader',
          options: {
            publicPath: isServerHot ? '/' : paths.publicPath
          }
        }
      ]
    },
    {
      test: /\.(png|jpg|jpeg|gif|ico)$/,
      use: [
        {
          loader: 'file-loader',
          options: {
            name() {
              return `images/${isProd ? '[name].[hash:8].[ext]': '[name].[ext]'}`
            },
          },
        },
      ],
    },
    {
      test: /\.(svg|eot|woff|woff2|ttf)$/,
      use: [
        {
          loader: 'file-loader',
          options: {
            name() {
              return `fonts/${isProd ? '[name].[hash:8].[ext]': '[name].[ext]'}`
            },
          },
        },
      ],
    },
    {
      test: /\.(less|css)$/,
      use: [
        {
          loader: isServerHot ? 'style-loader' : MiniCssExtractPlugin.loader
        },
        'css-loader',
        {
          loader: "less-loader",
          options: {
            javascriptEnabled: true,
            ...options.lessLoaderOptions
          }
        },
      ]
    },
    {
      test: /\.(_less)$/,
      use: [
        {
          loader: isServerHot ? 'style-loader' : MiniCssExtractPlugin.loader
        },
        {
          loader: 'css-loader',
          options: {
            importLoaders: 1,
            modules: true,
            localIdentName: '[path]_[name]_[local]_[hash:base64:5]'
          }
        },
        {
          loader: "less-loader",
          options: {
            javascriptEnabled: true,
            ...options.lessLoaderOptions
          }
        },
      ]
    },
    {
      test: /\.(js|jsx|ts|tsx)$/,
      include: [
        path.resolve(paths.srcPath),
        path.resolve(paths.roots, 'node_modules/@liepin'),
      ],
      use: jsLoader(),
    },
  ];
  if (esLint) {
    group.push({
      test: /\.(js|jsx|ts)$/,
      include: [
        path.resolve(paths.srcPath, 'components'),
        path.resolve(paths.srcPath, 'common'),
        path.resolve(paths.srcPath, 'pages'),
        path.resolve(paths.srcPath, 'views'),
      ],
      use: [
        {
          loader: 'eslint-loader',
          options: {
            failOnError: true,
          }
        },
      ],
    });
  }
  return group;
}

